﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperX.Common.Collection
{
    public static class CollectionExtensions
    {
        public static void AddRange<T>(this IList<T> list, IEnumerable<T> values)
        {
            if (values == null)
                return;
            foreach (T item in values)
            {
                list.Add(item);
            }
        }

        public static void AddRange<T>(this IList<T> list, params T[] values)
        {
            foreach (T item in values)
            {
                list.Add(item);
            }
        }

        public static float Average(this IList<int> array)
        {
            if (array.Count == 0)
            {
                throw new ArgumentOutOfRangeException("array");
            }
            float num = 0f;
            foreach (int t in array)
            {
                num += t;
            }
            return num / (float)array.Count;
        }

        public static float Average(this IList<float> array)
        {
            if (array.Count == 0)
            {
                throw new ArgumentOutOfRangeException("array");
            }
            float num = 0f;
            foreach (float t in array)
            {
                num += t;
            }
            return num / (float)array.Count;
        }

        public static T[] Combine<T>(this T[] target, params T[][] arrays)
        {
            T[] array = new T[arrays.Sum(x => x.Length) + target.Length];
            Array.Copy(target, 0, array, 0, target.Length);
            int num = target.Length;
            foreach (var sourceArray in arrays)
            {
                Array.Copy(sourceArray, 0, array, num, sourceArray.Length);
                num += sourceArray.Length;
            }
            return array;
        }

        public static int IndexOfNextContaining(this IList<string> items, string value, int startIndex)
        {
            for (int i = startIndex; i < items.Count; i++)
            {
                if (items[i].Contains(value))
                {
                    return i;
                }
            }
            return -1;
        }

        public static int IndexOfPreviousContaining(this IList<string> items, string value, int fromIndex)
        {
            for (int i = fromIndex - 1; i > -1; i--)
            {
                if (items[i].Contains(value))
                {
                    return i;
                }
            }
            return -1;
        }

        public static bool IsNullOrEmpty(this ICollection collection)
        {
            return collection == null || collection.Count == 0;
        }

        public static bool IsEmpty(this ICollection collection)
        {
            return collection.Count == 0;
        }

        public static bool IsNotEmpty(this ICollection collection)
        {
            return !collection.IsEmpty();
        }

        public static T Pop<T>(this IList<T> list)
        {
            if (list.Count == 0)
            {
                throw new Exception("Cannot pop items from an empty list.");
            }
            int index = list.Count - 1;
            T result = list[index];
            list.RemoveAt(index);
            return result;
        }

        public static void Push<T>(this IList<T> list, T value)
        {
            list.Add(value);
        }

        public static T[] RemoveDuplicates<T>(this T[] array)
        {
            Dictionary<T, T> dictionary = new Dictionary<T, T>();
            for (int i = 0; i < array.Length; i++)
            {
                if (!dictionary.ContainsKey(array[i]))
                {
                    dictionary.Add(array[i], array[i]);
                }
            }
            return dictionary.Values.ToArray<T>();
        }

        public static T[] Slice<T>(this T[] array, int start, int end)
        {
            if (end < 0)
            {
                end = array.Length - start - end - 1;
            }
            int num = end - start;
            var destinationArray = new T[num];
            Array.Copy(array, start, destinationArray, 0, num);
            return destinationArray;
        }
    }
}